package com.atguigu.dga.governance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceAssessGlobal;
import com.atguigu.dga.governance.bean.GovernanceAssessTecOwner;
import com.atguigu.dga.governance.service.GovernanceAssessDetailService;
import com.atguigu.dga.governance.service.GovernanceAssessGlobalService;
import com.atguigu.dga.governance.service.GovernanceAssessTecOwnerService;
import com.atguigu.dga.governance.service.MainGovernanceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * ClassName: MainGovernanceController
 * Package: com.atguigu.dga.governance.controller
 * Description:
 *
 * @Author: tubage
 * @Create: 2024/3/23 09:35
 * @Version: 1.0
 */
@RestController
@RequestMapping("/governance")
public class MainGovernanceController {
    @Autowired
    private GovernanceAssessGlobalService governanceAssessGlobalService;
    @Autowired
    private GovernanceAssessTecOwnerService governanceAssessTecOwnerService;
    @Autowired
    private GovernanceAssessDetailService governanceAssessDetailService;
    @Autowired
    private MainGovernanceService mainGovernanceService;

    // TODO 考评结果展示接口
    @GetMapping("/globalScore")
    public String globalScore() {
        GovernanceAssessGlobal governanceAssessGlobal = governanceAssessGlobalService.getOne(
                new QueryWrapper<GovernanceAssessGlobal>()
                        .inSql("assess_date", "select\n" +
                                "    max(assess_date)\n" +
                                "    from governance_assess_global")
        );
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("assessDate", governanceAssessGlobal.getAssessDate());
        jsonObj.put("sumScore", governanceAssessGlobal.getScore());
        jsonObj.put("scoreList", Arrays.asList(
                governanceAssessGlobal.getScoreSpec(),
                governanceAssessGlobal.getScoreStorage(),
                governanceAssessGlobal.getScoreCalc(),
                governanceAssessGlobal.getScoreQuality(),
                governanceAssessGlobal.getScoreSecurity()
        ));

        return jsonObj.toJSONString();
    }

    @GetMapping("/rankList")
    public String rankList() {
        List<Map<String, Object>> tecOwnerScoreList = governanceAssessTecOwnerService.listMaps(
                new QueryWrapper<GovernanceAssessTecOwner>()
                        .select("tec_owner as tecOwner", "score")
                        .inSql("assess_date", "select\n" +
                                "    max(assess_date)\n" +
                                "    from governance_assess_tec_owner")
                        .orderByDesc("score")
        );

        return JSON.toJSONString(tecOwnerScoreList);
    }

    @GetMapping("/problemList/{governType}/{pageNo}/{pageSize}")
    public String problemList(@PathVariable("governType") String governType,
                              @PathVariable("pageNo") Integer pageNo,
                              @PathVariable("pageSize") Integer pageSize) {

        List<GovernanceAssessDetail> governanceAssessDetailList =
                governanceAssessDetailService.getProblemListByTypeAndPage(governType, pageNo, pageSize);

        return JSONObject.toJSONString(governanceAssessDetailList);
    }

    @GetMapping("/problemNum")
    public String problemNum() {
        // listMaps
        List<Map<String, Object>> maps = governanceAssessDetailService.listMaps(
                new QueryWrapper<GovernanceAssessDetail>()
                        .select("governance_type", "count(*) as cnt")
                        .lt("assess_score", 10)
                        .inSql("assess_date", "select Max(assess_date) from governance_assess_detail")
                        .groupBy("governance_type")
        );
        JSONObject jsonObject = new JSONObject();
        for (Map<String, Object> map : maps) {
            jsonObject.put(map.get("governance_type").toString(), map.get("cnt"));
        }
        return jsonObject.toJSONString();
    }

    @PostMapping("/assess/{assessDate}")
    public String assess(@PathVariable("assessDate") String assessDate) {
        //
        try {
            mainGovernanceService.startAssess(assessDate);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "success";
    }
}
